package com.hnichr.ump.school.controller;


import com.hnichr.ump.common.school.entity.SchoolEnroll;
import com.hnichr.ump.common.school.service.SchoolEnrollService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Order;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/school/enroll/v1/")
@Tag(name = "SchoolEnrollController", description = "培训学校报名")
public class SchoolEnrollController {

    @Autowired
    private SchoolEnrollService schoolEnrollService;

    @GetMapping("list")
    @Operation(summary = "报名列表", description = "获取报名列表（无分页）", method = "GET")
    public Result<List<SchoolEnroll>> list(@Parameter(description="查询参数") Criteria c){
        List<SchoolEnroll> list = schoolEnrollService.list(SchoolEnroll.class, c);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "报名列表", description = "获取报名列表（分页）", method = "GET")
    public Result<Page<SchoolEnroll>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize,@Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        if(criteria!=null){
            criteria.addOrder(new Order("createTime",true));
        }
        Page<SchoolEnroll> list = schoolEnrollService.listPage(SchoolEnroll.class,page,criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取报名", description = "根据ID获取报名", method = "GET")
    public Result<SchoolEnroll> getById(@Parameter(name="ID") @PathVariable(name = "id", required = true) String id) {
        SchoolEnroll enroll = schoolEnrollService.getById(SchoolEnroll.class, id);
        return Result.OK(enroll);
    }

    @PostMapping("add")
    @Operation(summary = "新增报名", description = "新增报名", method = "POST")
    public Result<SchoolEnroll> add(@RequestBody SchoolEnroll enroll) {
        schoolEnrollService.save(enroll);
        return Result.OK(enroll);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑报名", description = "编辑报名", method = "PUT")
    public Result<SchoolEnroll> edit(@RequestBody SchoolEnroll enroll) {
        schoolEnrollService.update(enroll);
        return Result.OK(enroll);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除报名", description = "删除项目,多个id用英文逗号分隔", method = "DELETE")
    public Result<SchoolEnroll> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            schoolEnrollService.remove(SchoolEnroll.class, key);
        }
        return Result.OK();
    }
}
